package com.zhuky.exception;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 异常处理
 *
 */
@Slf4j
@Component
public class AuthExceptionResolver implements HandlerExceptionResolver {

        public ModelAndView resolveException(HttpServletRequest request,
                                             HttpServletResponse response, Object handler, Exception ex) {

            log.info("==============异常开始=============");
            //如果是shiro无权操作，因为shiro 在操作auno等一部分不进行转发至无权限url
            if(ex instanceof UnauthorizedException){
                ModelAndView mv = new ModelAndView("403.html");
                return mv;
            }
            ex.printStackTrace();
            ModelAndView mv = new ModelAndView("error.html");
            mv.addObject("exception", ex.toString().replaceAll("\n", "<br/>"));
            return mv;
        }

}
